﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace oLGame.Game
{
    public static class Engine
    {
        private static bool isSystemRunning = false;
        private static Queue<BaseSystemAction> systemQue = new Queue<BaseSystemAction>();
        public delegate void ActionHandler(BaseSystemAction action);
        public static event ActionHandler Action;

        public static void Tick()
        {
            try
            {
                BaseSystemAction action = systemQue.Dequeue();
                OnAction(action);
                action.Invoke();
            }
            catch (InvalidOperationException)
            {
                // Nothing to do, so just sleep a little bit. May need to be turned down to prevent latency.
                Thread.Sleep(10);
            }
        }

        public static void AddAction(BaseSystemAction action)
        {
            systemQue.Enqueue(action);
        }

        public static void OnAction(BaseSystemAction action)
        {
            if (Action != null)
            {
                Action(action);
            }
        }

        public static bool Running
        {
            get
            {
                return isSystemRunning;
            }
            set
            {
                isSystemRunning = value;
            }
        }
    }
}
